//
//  RVIntegralController.swift
//  AirTravel
//
//  Created by 许必成 on 2021/11/15.
//

import UIKit
import HandyJSON
class RVIntegralController: CommonVC {

    @IBOutlet weak var avaiLabel: UILabel!
    
    @IBOutlet weak var totalLabel: UILabel!
    
    @IBOutlet weak var tableView: UITableView!
    
    var dataSource: [RVMyIntgralList] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        head.title.text = "我的积分"
        confUI()
        getData()
    }
    
    func confUI(){
        tableView.rowHeight = 76
        tableView.register(UINib(nibName: "RVIntergralListCell", bundle: nil), forCellReuseIdentifier: "integral_list")
        tableView.reloadData()
    }
    
    func getData(){
        YProgressHUD.show()
        NetWorkRequest(API1.getMyIntegralInfo) { json in
            YProgressHUD.dismiss()
            guard let data = JSONDeserializer<RVMyIntgralData>.deserializeFrom(dict: json.dictionaryObject) else{
                return
            }
            
            self.bindData(data: data)
            
        } failed: { msg in
            YProgressHUD.dismiss()

        } errorResult: {
            YProgressHUD.dismiss()
        }
    }
    
    func bindData(data: RVMyIntgralData){
        self.avaiLabel.text = data.data.available_score
        self.totalLabel.text = data.data.total_score
        self.dataSource = data.data.list
        self.tableView.reloadData()
    }


}


extension RVIntegralController: UITableViewDelegate, UITableViewDataSource{
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "integral_list", for: indexPath) as! RVIntergralListCell
        let list = dataSource[indexPath.row]
        cell.nameLabel.text = list.memo
        cell.countLabel.text = list.score
        cell.timeLabel.text = list.createtime.formatTimeStamp()
        
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

